import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SoldadoBasooka here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SoldadoBasooka extends Enemigo
{
    private GreenfootImage image;
    int delay=0;
    private GreenfootImage [] soldier = new GreenfootImage[13];
    
    private GreenfootImage [] soldierWalk = new GreenfootImage[12];
    
    private GreenfootImage [] soldierRight = new GreenfootImage[13];
    
    private GreenfootImage [] soldierWalkRight = new GreenfootImage[12];
    private boolean disparo = false;
    private int cont=0;
    private int tiempo=0;
    private int direccion  = -1;
    private int totalPasos;
    
    private int pasos=0;
    int width=72;
    int height=108;
    /**
     * Act - do whatever the Enemigo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public SoldadoBasooka(int x0,int xf, int n)
    {
        super(5,x0,xf,n);
        totalPasos=this.numGenerator();
        for(int i=1; i<= soldier.length;i++)
    {
        soldier[i-1] = new GreenfootImage("Soldier2_"+i+".png");
        soldier[i-1].scale(width,height);
        soldier[i-1].mirrorHorizontally();
    }
       for(int i=1; i<= soldierWalk.length;i++)
    {
        soldierWalk[i-1] = new GreenfootImage("soldierWalk"+i+".png");
        soldierWalk[i-1].scale(width,height);
       // soldier[i-1].mirrorHorizontally();
    }
    
    for(int i=1; i<= soldierRight.length;i++)
    {
        soldierRight[i-1] = new GreenfootImage("Soldier2_"+i+".png");
        soldierRight[i-1].scale(width,height);
        //soldierRight[i-1].mirrorHorizontally();
    }
       for(int i=1; i<= soldierWalkRight.length;i++)
    {
        soldierWalkRight[i-1] = new GreenfootImage("soldierWalk"+i+".png");
        soldierWalkRight[i-1].scale(width,height);
        soldierWalkRight[i-1].mirrorHorizontally();
    }
    
    setImage(soldierWalk[cont]);
    
    }
    public void act()
    {
        
        if(disparo)
          dispara();
        else
        {
            walk();
        } 
        super.act();
        
        
    }
    public void dispara() 
    { 
            if(delay==10)
            {
                if(direccion==1){
            setImage(soldierRight[cont]);
            }
            else
            {
                setImage(soldier[cont]);
            }
            
            cont++;
            delay=0;
            
            if(cont==soldier.length-1)
            {
                setLocation(getX(),getY()-10);
                cont=0;
                disparo=false;
                pasos=0;
                direccion*=-1;
                
            }
            
            if(cont==7)
            {
                getWorld().addObject(new misil(direccion),getX()+(direccion*40),getY());
            }
        
        }
        else
        {
            delay++;
        }
    }
        // Add your action code here.
        
    public void walk()
    {
    
            
            if(delay==10)
            {
            //cont++;
            if(super.getNivel()==1)
            {
             this.chacaLimite(); 
            }
            else
            {
                this.generaDireccion();
            }
            setLocation(getX()+(4*direccion),getY());
            if(direccion==1)
            {
            setImage(soldierWalkRight[cont]);
           }
           else
           {
               setImage(soldierWalk[cont]);
            }
            cont++;
            
            delay=0;
            if(cont==11)
            {
                
                cont=0;
                pasos++;
                if(pasos==totalPasos)
                {
                    disparo=true;
                    setLocation(getX(),getY()+10);
                    totalPasos=this.numGenerator();
                }
            }
        }
        else
        delay++;
    }
    
    private int numGenerator()
    { //Generates a random number with easy to influence min and max
        int num = 0;
        while (!(num >= 1 && num <= 5)){
            num = (int)(Math.random()*10);
        }
        return num; 
    }
     public void chacaLimite()
   {
       if(super.Limite())
       {
           this.cambiaDiereccion();
        }
   }
   
   public void cambiaDiereccion()
   {
       direccion=direccion*-1;
   }
    
    public void generaDireccion()
    {
        if(((FlagWarWorld)getWorld()).getJugador() )
        {
          int x1=getX();
          int x2=((FlagWarWorld)getWorld()).getXJugador() ;
          if(x2-x1<=0)
          {
              direccion=-1;
          }
            else
            {
                direccion=1;
            }
        
        }    
    }
}